package com.yueban.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yueban.R;
import com.yueban.model.BaseResponse;
import com.yueban.model.ResultCode;
import com.yueban.net.NetWorkController;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class FeadBackActivity extends AppCompatActivity {


    private EditText etName;
    private EditText etQQ;
    private EditText suggestion;
    private Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fead_back);

        Toolbar toolbar = (Toolbar)findViewById(R.id.feadback_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        //添加返回键
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("用户反馈");
        }

        etName = (EditText) findViewById(R.id.etName);
        etQQ = (EditText) findViewById(R.id.etQQ);
        suggestion = (EditText) findViewById(R.id.etSuggestion);
        submit = (Button) findViewById(R.id.btnOk);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (suggestion.getText().toString().trim().equals(""))
                    return;
                NetWorkController netWorkController = new NetWorkController();
                netWorkController.submitFeedback(etName.getText().toString().trim(),
                        etQQ.getText().toString().trim(),
                        suggestion.getText().toString().trim()).subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseResponse baseResponse) {
                        if (baseResponse.getStatus() == ResultCode.SUCCESS) {
                            Snackbar.make(submit, "感谢您的支持", Snackbar.LENGTH_SHORT).show();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {

                                    finish();
                                }
                            }, 1000);
                        }else {
                                finish();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(FeadBackActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }
        });


    }
}
